package com.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yiyang.constant.ResultCode;
import com.yiyang.domain.Result;
import com.yiyang.domain.po.NurseContent;
import com.yiyang.domain.po.NurseLevel;
import com.yiyang.domain.po.NurseLevelItem;
import com.yiyang.service.NurseLevelItemService;
import com.yiyang.service.NurseLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nurseLevels")
public class NurseLevelController {

    @Autowired
    private NurseLevelService nurseLevelService;

    @Autowired
    private NurseLevelItemService nurseLevelItemService;



    @GetMapping("/listNurseLevel")
    public Result<List<NurseLevel>> listNurseLevel(NurseLevel nurseLevel){
        List<NurseLevel> list = nurseLevelService.list(new LambdaQueryWrapper<NurseLevel>().eq(NurseLevel::getLevelStatus, nurseLevel.getLevelStatus()));

        return Result.success(list);

    }

    @PostMapping("/addNurseLevel")
    public Result addNurseLevel(@RequestBody NurseLevel nurseLevel){
        boolean flag = nurseLevelService.save(nurseLevel);
        if (flag){
            return Result.success();
        }else {
            return Result.fail(ResultCode.ERROR_NURSE_LEVEL_ADD,"新增护理级别失败");
        }
    }

    @PutMapping("/updateNurseLevel")
    public Result updateNurseLevel(@RequestBody NurseLevel nurseLevel){
        nurseLevel.setIsDeleted(null);
        nurseLevel.setLevelName(null);
        boolean flag = nurseLevelService.updateById(nurseLevel);
        if (flag){
            return Result.success();
        }else {
            return Result.fail(ResultCode.ERROR_NURSE_LEVEL_UPDATE,"更新护理级别失败");
        }
    }

    @GetMapping("/listNurseItemByLevel")
    public Result<List<NurseContent>> listNurseItemByLevel(Integer levelId){
        List<NurseContent> nurseContentList = nurseLevelService.listNurseItemByLevel(levelId);

        return Result.success(nurseContentList);

    }

    // 根据护理级别id和护理项目id删除中间表中的关联信息
    @DeleteMapping("/removeNurseLevelItem")
    public Result removeNurseLevelItem(Integer itemId,Integer levelId){
        boolean flag = nurseLevelItemService.remove(new LambdaQueryWrapper<NurseLevelItem>().eq(NurseLevelItem::getLevelId, itemId).eq(NurseLevelItem::getLevelId, levelId));
        if (flag){
            return Result.success();
        }else {
            return Result.fail(ResultCode.ERROR_NURSE_LEVEL_DELETE,"删除护理级别下方的护理项目失败");
        }

    }

    @PutMapping("/addItemToLevel")
    public Result addItemToLevel(@RequestBody NurseLevelItem nurseLevelItem){
        boolean flag = nurseLevelItemService.save(nurseLevelItem);
        if (flag){
            return Result.success();
        }else {
            return Result.fail(ResultCode.ERROR_NURSE_LEVEL_ITEM_ADD,"添加护理级别下方的护理项目失败");
        }
    }
}
